//
//  WLRouterNetworkSetController.m
//  WanLian
//
//  Created by muzhuang on 2018/10/22.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLRouterNetworkSetController.h"
//#import "WLRouterNetSetCell.h"
#import "WLBindingRouterSetCell.h"
#import "JXCategoryView.h"

@interface WLRouterNetworkSetController ()<UITableViewDelegate,UITableViewDataSource,JXCategoryViewDelegate>

@property (nonatomic, strong) UITableView *tbRouterSet;
@property (nonatomic, strong) UIButton  *btnDone;
@property (nonatomic, assign) NSInteger openFlag;//0:section0 open 1:close

//@property (nonatomic, strong) UIView *vNetworkBack;
@property (nonatomic, strong) UITableView *tbNetwork;
@property (nonatomic, assign) NSInteger netType;//0:pppoe,1:静,2:动
@property (nonatomic, assign) NSInteger netConfig;//0:auto,1:manual
@property (nonatomic, assign) BOOL  isFirstFinished;//是否完成联网设置

@property (nonatomic, strong) UITableView *tbWiFiSet;
@property (nonatomic, assign) NSInteger wifiType;//0:2.4g,1:5g,2:访客
@property (nonatomic, assign) NSInteger encrypType;//0:WPA2,1:WPA/WPA2,2:无密码
@property (nonatomic, assign) BOOL  isHide;//隐藏网络
@property (nonatomic, strong) NSMutableDictionary *dictTwo;
@property (nonatomic, strong) NSMutableDictionary *dictFive;
@property (nonatomic, strong) NSMutableDictionary *dictGuest;

@end

@implementation WLRouterNetworkSetController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSString *wifiStr = [WLCommonTool getWifiSSID];
    if (![wifiStr hasPrefix:@"TianYiLianWiFi"])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"网络配置";
    
    _dictTwo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"hasTwoConfig",@"psk2",@"twoWifiLocktype",@(NO),@"twoWifiForce", nil];
    _dictFive = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"hasFiveConfig",@"psk2",@"fiveWifiLocktype",@(NO),@"fiveWifiForce", nil];
    _dictGuest = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"hasCustomerConfig",@"psk2",@"customerWifiLocktype",@(NO),@"customerWifiForce", nil];
    WLWkSelf
    UILabel *labWiFi = [[UILabel alloc] init];
    labWiFi.font = [WLCommonTool fontTypePingFangSCRegular:12];
    labWiFi.textColor = wlUIColorRGB(0x29A8FA);
    [self.view addSubview:labWiFi];
    NSString *wifiStr = [WLCommonTool getWifiSSID];
    labWiFi.text = [NSString stringWithFormat:@"当前路由器WiFi网络为：%@",wifiStr];
    [labWiFi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(12);
        make.left.equalTo(weakSelf.view).offset(WLScaleX(16));
        make.height.equalTo(@(20));
    }];
    
    _btnDone = [[UIButton alloc] init];
    _btnDone.backgroundColor = wlUnderlineColor;
    _btnDone.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:16];
    [_btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btnDone addTarget:self action:@selector(setDoneAction) forControlEvents:UIControlEventTouchUpInside];
    [_btnDone setTitle:@"完成" forState:UIControlStateNormal];
    [self.view addSubview:_btnDone];
    [_btnDone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.view).offset(-WL_Bottom);
        make.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@(WLScaleX(49)));
    }];
    _btnDone.enabled = NO;
    
    _tbRouterSet = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tbRouterSet.delegate = self;
    _tbRouterSet.dataSource = self;
    if (@available(iOS 11.0, *)) {
        _tbRouterSet.estimatedSectionHeaderHeight = 0;
        _tbRouterSet.estimatedSectionFooterHeight = 0;
    }
//    _tbRouterSet.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tbRouterSet];
    _tbRouterSet.scrollEnabled = NO;
    
    [_tbRouterSet mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(44);
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.btnDone.mas_top);
    }];
    
//    [self settingNetwork];
}

- (void)updateBtn
{
    [WLCommonTool addGradientColor:_btnDone fromColor:wlblueColor toColor:wlUIColorRGB(0x2292F5)];
    self.btnDone.enabled = YES;
}

- (UITableView *)tbNetwork
{
    if (!_tbNetwork) {
        _tbNetwork = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, self.tbRouterSet.frame.size.height-WLScaleX(112+20)) style:UITableViewStyleGrouped];
        _tbNetwork.delegate = self;
        _tbNetwork.dataSource = self;
        _tbNetwork.rowHeight = WLScaleX(40);
        if (@available(iOS 11.0, *)) {
            _tbNetwork.estimatedSectionHeaderHeight = 0;
            _tbNetwork.estimatedSectionFooterHeight = 0;
        }
        _tbNetwork.scrollEnabled = NO;
    }
    return _tbNetwork;
}

- (UITableView *)tbWiFiSet
{
    if (!_tbWiFiSet) {
        _tbWiFiSet = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, WLScaleX(180)) style:UITableViewStylePlain];
        _tbWiFiSet.delegate = self;
        _tbWiFiSet.dataSource = self;
        _tbWiFiSet.rowHeight = WLScaleX(40);
        if (@available(iOS 11.0, *)) {
            _tbWiFiSet.estimatedSectionHeaderHeight = 0;
            _tbWiFiSet.estimatedSectionFooterHeight = 0;
        }
        _tbWiFiSet.scrollEnabled = NO;
    }
    return _tbWiFiSet;
}

//- (void)settingNetwork
//{
//    _vNetworkBack = [[UIView alloc] init];
//    _tbNetwork = [[UITableView alloc] initWithFrame:_vNetworkBack.bounds style:UITableViewStyleGrouped];
//    _tbNetwork.delegate = self;
//    _tbNetwork.dataSource = self;
//    _tbNetwork.rowHeight = WLScaleX(40);
//    if (@available(iOS 11.0, *)) {
//        _tbNetwork.estimatedSectionHeaderHeight = 0;
//        _tbNetwork.estimatedSectionFooterHeight = 0;
//    }
//    [_vNetworkBack addSubview:_tbNetwork];
//    _tbNetwork.scrollEnabled = NO;
//
//}

#pragma -- mark tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([tableView isEqual:_tbRouterSet]) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:_tbRouterSet])
    {
        if (section==0) {
            if (_openFlag==0) {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            if (_openFlag==1) {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
    else if ([tableView isEqual:self.tbNetwork])
    {
        if (self.netType==0) {
            return 2;
        }
        else if (_netType==1)
        {
            return 5;
        }
        return 3;
    }
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([tableView isEqual:_tbRouterSet])
    {
        if (_openFlag==0) {
            return 0.01;
        }
        return WLScaleX(20);
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([tableView isEqual:_tbRouterSet]) {
        return WLScaleX(56);
    }
    else if ([tableView isEqual:self.tbNetwork])
    {
        return WLScaleX(40);
    }
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:_tbRouterSet])
    {
        if (indexPath.section==0)
        {
            if (_openFlag==0)
            {
                return self.tbRouterSet.frame.size.height-WLScaleX(112+20);
//                if (self.netType==0) {
//                    return WLScaleX(225);
//                }
//                return WLScaleX(265);
            }
            else
            {
                return 0;
            }
        }
        else
        {
            if (_openFlag==1)
            {
                return WLScaleX(220);
            }
            else
            {
                return 0;
            }
        }
    }
    else if ([tableView isEqual:self.tbNetwork])
    {
        return WLScaleX(40);
    }
    return WLScaleX(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ([tableView isEqual:self.tbNetwork])
    {
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, WLScaleX(40))];
        headerView.backgroundColor = [UIColor whiteColor];
        NSArray *arrTitles = @[@" PPPoE",@" 静态IP",@" 动态IP"];
//        CGFloat width = (WIDTH_SCREEN_WL-3*70)/4.0;
        for (int i=0; i<arrTitles.count; i++)
        {
            UIButton *btnType = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16)+i*(75+WLScaleX(20)), 0, 75, WLScaleX(40))];
            [btnType setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
            [btnType setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
            btnType.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
            [btnType setTitleColor:wlDarkGrayColor forState:UIControlStateNormal];
            [btnType setTitle:arrTitles[i] forState:UIControlStateNormal];
            btnType.tag = 100+i;
            [btnType addTarget:self action:@selector(selectNetTypeAction:) forControlEvents:UIControlEventTouchUpInside];
            if (self.netType==i) {
                btnType.selected = YES;
            }
            [headerView addSubview:btnType];
        }
        return headerView;
    }
    else if ([tableView isEqual:self.tbWiFiSet])
    {
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, WLScaleX(60))];
        headerView.backgroundColor = [UIColor whiteColor];
        NSArray *arrTitles = @[@" WPA2",@" WPA/WPA2",@" 无密码"];
        for (int i=0; i<arrTitles.count; i++)
        {
            UIButton *btnType = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16)+i*(80+WLScaleX(15))+(i>1?25:0), WLScaleX(10), 80+(i==1?25:0), WLScaleX(40))];
            [btnType setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
            [btnType setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
            btnType.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
            [btnType setTitleColor:wlDarkGrayColor forState:UIControlStateNormal];
            [btnType setTitle:arrTitles[i] forState:UIControlStateNormal];
            btnType.tag = 300+i;
            [btnType addTarget:self action:@selector(encrypTypeAction:) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:btnType];
            
            int j = 0;
            if (_wifiType==0) {
                if ([_dictTwo[@"twoWifiLocktype"] isEqualToString:@"psk-mixed"]) {
                    j=1;
                }
                else if ([_dictTwo[@"twoWifiLocktype"] isEqualToString:@"none"])
                {
                    j=2;
                }
            }
            else if (_wifiType==1)
            {
                if ([_dictFive[@"fiveWifiLocktype"] isEqualToString:@"psk-mixed"]) {
                    j=1;
                }
                else if ([_dictFive[@"fiveWifiLocktype"] isEqualToString:@"none"])
                {
                    j=2;
                }
            }
            else
            {
                if ([_dictGuest[@"customerWifiLocktype"] isEqualToString:@"psk-mixed"]) {
                    j=1;
                }
                else if ([_dictGuest[@"customerWifiLocktype"] isEqualToString:@"none"])
                {
                    j=2;
                }
            }
            if (i==j) {
                btnType.selected = YES;
            }
        }
        return headerView;
    }
    else
    {
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, WLScaleX(56))];
        headerView.backgroundColor = [UIColor whiteColor];
        UILabel *labTitle = [[UILabel alloc]initWithFrame:CGRectMake(WLScaleX(32), WLScaleX(13), 150, WLScaleX(30))];
        labTitle.font = [WLCommonTool fontTypePingFangSCMedium:17];
        labTitle.textColor = wlBlackColor;
        [headerView addSubview:labTitle];
        UIImageView *selectImageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(32)-14, (WLScaleX(56)-9)/2.0, 14, 9)];
        [headerView addSubview:selectImageView];
        if (section==0)
        {
            labTitle.text = @"联网设置(第一步)";
            if (_openFlag==0) {
                selectImageView.image = [UIImage imageNamed:@"arrow_up"];
            }
            else
            {
                selectImageView.image = [UIImage imageNamed:@"arrow_down"];
            }
        }
        else
        {
            labTitle.text = @"WiFi设置(第二步)";
            if (_openFlag==0) {
                selectImageView.image = [UIImage imageNamed:@"arrow_down"];
            }
            else
            {
                selectImageView.image = [UIImage imageNamed:@"arrow_up"];
            }
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [headerView addGestureRecognizer:tap];
        headerView.tag = section;
        return headerView;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    if ([tableView isEqual:self.tbNetwork])
    {
        static NSString *strNetNew = @"newNetCell";
        WLBindingRouterSetCell *cell = [tableView dequeueReusableCellWithIdentifier:strNetNew];
        if (!cell) {
            cell = [[WLBindingRouterSetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strNetNew];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.textNetInfo.enabled = YES;
        cell.accessoryView = nil;
        if (self.netType==0)
        {
            cell.labTitle.frame = CGRectMake(WLScaleX(16), WLScaleX(5), 35, WLScaleX(30));
            cell.textNetInfo.frame = CGRectMake(WLScaleX(16)+40, WLScaleX(2), 200, WLScaleX(36));
            if (row==0) {
                cell.labTitle.text = @"账号";
                cell.textNetInfo.placeholder = @"请输入宽带账号";
            }
            else
            {
                cell.labTitle.text = @"密码";
                cell.textNetInfo.placeholder = @"请输入宽带密码";
                cell.textNetInfo.secureTextEntry = YES;
                UIButton *btnEye = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, WLScaleX(40))];
                [btnEye setImage:[UIImage imageNamed:@"eye_net_close"] forState:UIControlStateNormal];
                [btnEye setImage:[UIImage imageNamed:@"eye_net_open"] forState:UIControlStateSelected];
                [btnEye addTarget:self action:@selector(netOpenOrCloseAction:) forControlEvents:UIControlEventTouchUpInside];
                btnEye.selected = NO;
                cell.accessoryView = btnEye;
            }
        }
        else if (_netType==1)
        {
            cell.labTitle.frame = CGRectMake(WLScaleX(16), WLScaleX(5), 65, WLScaleX(30));
            cell.textNetInfo.frame = CGRectMake(WLScaleX(16)+70, WLScaleX(2), 200, WLScaleX(36));
            if (row==0) {
                cell.labTitle.text = @"网络地址";
                cell.textNetInfo.placeholder = @"请输入网络地址";
            }
            else if (row==1)
            {
                cell.labTitle.text = @"子网掩码";
                cell.textNetInfo.placeholder = @"请输入子网掩码";
            }
            else if (row==2)
            {
                cell.labTitle.text = @"默认网关";
                cell.textNetInfo.placeholder = @"请输入默认网关";
            }
            else if (row==3)
            {
                cell.labTitle.text = @"首选DNS";
                cell.textNetInfo.placeholder = @"请输入首选DNS";
                UILabel *labMust = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
                labMust.font = [WLCommonTool fontTypePingFangSCRegular:12];
                labMust.textColor = wlUnderlineColor;
                labMust.textAlignment = NSTextAlignmentCenter;
                labMust.text = @"必填";
                cell.accessoryView = labMust;
            }
            else
            {
                cell.labTitle.text = @"备选DNS";
                cell.textNetInfo.placeholder = @"请输入备选DNS";
                UILabel *labOption = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
                labOption.font = [WLCommonTool fontTypePingFangSCRegular:12];
                labOption.textColor = wlUnderlineColor;
                labOption.textAlignment = NSTextAlignmentCenter;
                labOption.text = @"选填";
                cell.accessoryView = labOption;
            }
        }
        else
        {
//            cell.textNetInfo.placeholder = @"";
//            cell.textNetInfo.enabled = NO;
            if (row==0)
            {
                static NSString *strConfig = @"cellConfig";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strConfig];
                if (!cell) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strConfig];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                for (UIView *subView in cell.contentView.subviews) {
                    [subView removeFromSuperview];
                }
                NSArray *arrTitles = @[@" 自动配置DNS",@" 手动配置DNS"];
                for (int i=0; i<arrTitles.count; i++)
                {
                    UIButton *btnType = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16)+i*(100+WLScaleX(20)), 0, 100, WLScaleX(40))];
                    [btnType setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
                    [btnType setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
                    btnType.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:12];
                    [btnType setTitleColor:wlDarkGrayColor forState:UIControlStateNormal];
                    [btnType setTitle:arrTitles[i] forState:UIControlStateNormal];
                    btnType.tag = 200+i;
                    [btnType addTarget:self action:@selector(selectNetConfigAction:) forControlEvents:UIControlEventTouchUpInside];
                    if (self.netConfig==i) {
                        btnType.selected = YES;
                    }
                    [cell.contentView addSubview:btnType];
                }
                return cell;
            }
            else
            {
                cell.labTitle.frame = CGRectMake(WLScaleX(16), WLScaleX(5), 65, WLScaleX(30));
                cell.textNetInfo.frame = CGRectMake(WLScaleX(16)+70, WLScaleX(2), 200, WLScaleX(36));
                if (_netConfig==0)
                {
                    if (row==1)
                    {
                        cell.labTitle.text = @"首选DNS";
                        cell.textNetInfo.placeholder = @"系统自动分配DNS";
                    }
                    else
                    {
                        cell.labTitle.text = @"备选DNS";
                        cell.textNetInfo.placeholder = @"系统自动分配DNS";
                    }
                }
                else
                {
                    if (row==1)
                    {
                        cell.labTitle.text = @"首选DNS";
                        cell.textNetInfo.placeholder = @"请输入首选DNS";
                        UILabel *labMust = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
                        labMust.font = [WLCommonTool fontTypePingFangSCRegular:12];
                        labMust.textColor = wlUnderlineColor;
                        labMust.textAlignment = NSTextAlignmentCenter;
                        labMust.text = @"必填";
                        cell.accessoryView = labMust;
                    }
                    else
                    {
                        cell.labTitle.text = @"备选DNS";
                        cell.textNetInfo.placeholder = @"请输入备选DNS";
                        UILabel *labOption = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
                        labOption.font = [WLCommonTool fontTypePingFangSCRegular:12];
                        labOption.textColor = wlUnderlineColor;
                        labOption.textAlignment = NSTextAlignmentCenter;
                        labOption.text = @"选填";
                        cell.accessoryView = labOption;
                    }
                }
            }
        }
        return cell;
    }
    else if ([tableView isEqual:self.tbWiFiSet])
    {
        if (row<2) {
            static NSString *strNet = @"netCell";
            WLBindingRouterSetCell *cell = [tableView dequeueReusableCellWithIdentifier:strNet];
            if (!cell) {
                cell = [[WLBindingRouterSetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strNet];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.textNetInfo.enabled = YES;
            cell.textNetInfo.text = @"";
            cell.accessoryView = nil;
            if (row==0) {
                cell.labTitle.text = @"名称";
                cell.textNetInfo.placeholder = @"请设置WiFi名称";
                if (_wifiType==0) {
                    cell.textNetInfo.text = _dictTwo[@"twoWifiAccount"];
                }
                else if (_wifiType==1)
                {
                    cell.textNetInfo.text = _dictFive[@"fiveAccount"];
                }
                else
                {
                    cell.textNetInfo.text = _dictGuest[@"customerWifiAccount"];
                }
            }
            else
            {
                cell.labTitle.text = @"密码";
                cell.textNetInfo.placeholder = @"请设置WiFi密码";
                cell.textNetInfo.secureTextEntry = YES;
                if (_wifiType==0) {
                    cell.textNetInfo.text = _dictTwo[@"twoWifiPassWord"];
                }
                else if (_wifiType==1)
                {
                    cell.textNetInfo.text = _dictFive[@"fiveWifiPassWord"];
                }
                else
                {
                    cell.textNetInfo.text = _dictGuest[@"customerWifiPassWord"];
                }
                
                UIButton *btnEye = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
                [btnEye setImage:[UIImage imageNamed:@"eye_net_close"] forState:UIControlStateNormal];
                [btnEye setImage:[UIImage imageNamed:@"eye_net_open"] forState:UIControlStateSelected];
                [btnEye addTarget:self action:@selector(eyeAction:) forControlEvents:UIControlEventTouchUpInside];
                btnEye.selected = NO;
                cell.accessoryView = btnEye;
            }
            return cell;
        }
        
        static NSString *strWiFi = @"WiFiCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strWiFi];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strWiFi];
            cell.textLabel.font = [WLCommonTool fontTypePingFangSCRegular:17];
            cell.textLabel.textColor = wlBlackColor;
            cell.detailTextLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
            cell.detailTextLabel.textColor = wlLightTextColor;
        }
        cell.textLabel.text = @"隐藏网络";
        cell.detailTextLabel.text = @"";
        UISwitch *btnSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, WLScaleX(14))];
        btnSwitch.onTintColor = wlblueColor;//wlRGBACOLOR(26, 89, 226, 0.6);
//        btnSwitch.thumbTintColor = wlblueColor;
        [btnSwitch addTarget:self action:@selector(hideNetworkAction:) forControlEvents:UIControlEventValueChanged];
        btnSwitch.on = _isHide;
        cell.accessoryView = btnSwitch;
        if (_wifiType==0) {
            btnSwitch.on = [_dictTwo[@"twoWifiForce"] boolValue];
        }
        else if (_wifiType==1)
        {
            btnSwitch.on = [_dictFive[@"fiveWifiForce"] boolValue];
        }
        else
        {
            btnSwitch.on = [_dictGuest[@"customerWifiForce"] boolValue];
        }
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    static NSString *strCell = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    if (indexPath.section==0)
    {
//        CGFloat height = WLScaleX(265);
//        if (self.netType==0) {
//            height = WLScaleX(185);
//        }
        CGFloat height = self.tbRouterSet.frame.size.height-WLScaleX(112+20);
//        if (self.netType==0) {
//            height = WLScaleX(185);
//        }
        UIView *vNetworkBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, height)];
        vNetworkBack.backgroundColor = wlUIColorRGB(0xFAFAFA);
        self.tbNetwork.frame = vNetworkBack.bounds;
//        _tbNetwork = [[UITableView alloc] initWithFrame:vNetworkBack.bounds style:UITableViewStyleGrouped];
//        _tbNetwork.delegate = self;
//        _tbNetwork.dataSource = self;
//        _tbNetwork.rowHeight = WLScaleX(40);
//        if (@available(iOS 11.0, *)) {
//            _tbNetwork.estimatedSectionHeaderHeight = 0;
//            _tbNetwork.estimatedSectionFooterHeight = 0;
//        }
//        _tbNetwork.scrollEnabled = NO;
        [vNetworkBack addSubview:_tbNetwork];
        
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, vNetworkBack.bounds.size.width, WLScaleX(62))];
        footerView.backgroundColor = [UIColor whiteColor];
        UIButton *connectBtn = [[UIButton alloc]initWithFrame:CGRectMake((vNetworkBack.bounds.size.width-WLScaleX(180))/2.0, WLScaleX(16), WLScaleX(180), WLScaleX(30))];
        [connectBtn setBackgroundColor:[UIColor whiteColor]];
        connectBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:14];
        [connectBtn setTitleColor:wlUIColorRGB(0x29A8FA) forState:UIControlStateNormal];
        [connectBtn setTitle:@"拨号连接" forState:UIControlStateNormal];
        [connectBtn addTarget:self action:@selector(connectSaveAction) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:connectBtn];
        connectBtn.layer.cornerRadius = 5;
        connectBtn.layer.masksToBounds = YES;
        connectBtn.layer.borderWidth = 1;
        connectBtn.layer.borderColor = wlUIColorRGB(0x29A8FA).CGColor;
        _tbNetwork.tableFooterView = footerView;
        [cell.contentView addSubview:vNetworkBack];
    }
    else
    {
        NSArray *titleNameArr = @[@"2.4G",@"5G",@"访客网络"];
        
        CGFloat height = WLScaleX(180);
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, WLScaleX(40), WIDTH_SCREEN_WL, height)];
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(WIDTH_SCREEN_WL*(titleNameArr.count), height);
        scrollView.bounces = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        [cell.contentView addSubview:scrollView];
        
        JXCategoryTitleView *categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, WLScaleX(40))];
        categoryView.backgroundColor = [UIColor whiteColor];
        categoryView.delegate = self;
        categoryView.titles = titleNameArr;
        categoryView.titleColor = wlLightTextColor;
        categoryView.titleSelectedColor = wlBlackColor;
        categoryView.cellWidth = WLScaleX(75);
        categoryView.titleFont = [WLCommonTool fontTypePingFangSCRegular:16];
        //2、添加并配置指示器
        //lineView
        JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
        lineView.indicatorLineViewColor = wlblueColor;
        lineView.indicatorLineWidth = WLScaleX(50);//JXCategoryViewAutomaticDimension;
        lineView.lineStyle = JXCategoryIndicatorLineStyle_IQIYI;
        categoryView.indicators = @[lineView];
        
        //3、绑定contentScrollView。self.scrollView的初始化细节参考源码。
        categoryView.contentScrollView = scrollView;
        [cell.contentView addSubview:categoryView];
        
        self.tbWiFiSet.frame = CGRectMake(0, 0, WIDTH_SCREEN_WL, height);
        [scrollView addSubview:self.tbWiFiSet];
//        for (int i = 0;  i<titleNameArr.count; i++) {
//            self.tbWiFiSet.frame = CGRectMake(WIDTH_SCREEN_WL*i, 0, WIDTH_SCREEN_WL, height);
//            [scrollView addSubview:self.tbWiFiSet];
//            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL*i, 0, WIDTH_SCREEN_WL, height)];
//            if (i==0) {
//                view.backgroundColor = [UIColor redColor];
//            }
//            else if (i==1)
//            {
//                view.backgroundColor = [UIColor blueColor];
//            }
//            else
//            {
//                view.backgroundColor = [UIColor blackColor];
//            }
//            [scrollView addSubview:view];
//        }
//        categoryView.defaultSelectedIndex = 2;
    }
    return cell;
}

#pragma mark - networkSetting
- (void)selectNetTypeAction:(UIButton *)btnType
{
    self.netType = btnType.tag-100;
    [self.tbNetwork reloadData];
}

- (void)netOpenOrCloseAction:(UIButton *)btnEye
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    WLBindingRouterSetCell *cell = (WLBindingRouterSetCell *)[self.tbNetwork cellForRowAtIndexPath:indexPath];
    cell.textNetInfo.secureTextEntry = btnEye.selected;
    btnEye.selected = !btnEye.selected;
}

- (void)selectNetConfigAction:(UIButton *)btnConfig
{
    self.netConfig = btnConfig.tag-200;
    [self.tbNetwork reloadData];
}

#pragma mark - WiFi set
- (void)encrypTypeAction:(UIButton *)btnType
{
    self.encrypType = btnType.tag-300;
    
    NSString *strWap = @"psk2";
    if (_encrypType==2) {
        strWap = @"none";
    }
    else if (_encrypType==1)
    {
        strWap = @"psk-mixed";
    }
    if (_wifiType==0) {
        _dictTwo[@"twoWifiLocktype"] = strWap;
    }
    else if (_wifiType==1)
    {
        _dictFive[@"fiveWifiLocktype"] = strWap;
    }
    else
    {
        _dictGuest[@"customerWifiLocktype"] = strWap;
    }
    
    [self saveWiFiConfig];
    
    [self.tbWiFiSet reloadData];
}

- (void)eyeAction:(UIButton *)eyeBtn
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    WLBindingRouterSetCell *cell = (WLBindingRouterSetCell *)[self.tbWiFiSet cellForRowAtIndexPath:indexPath];
    cell.textNetInfo.secureTextEntry = eyeBtn.selected;
    eyeBtn.selected = !eyeBtn.selected;
}

- (void)hideNetworkAction:(UISwitch *)swtHide
{
    _isHide = swtHide.on;
    if (_wifiType==0) {
        _dictTwo[@"twoWifiForce"] = @(_isHide);
    }
    else if (_wifiType==1)
    {
        _dictFive[@"fiveWifiForce"] = @(_isHide);
    }
    else
    {
        _dictGuest[@"customerWifiForce"] = @(_isHide);
    }
}

- (void)saveWiFiConfig
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    WLBindingRouterSetCell *cell = (WLBindingRouterSetCell *)[self.tbWiFiSet cellForRowAtIndexPath:indexPath];
    NSString *strName= cell.textNetInfo.text;
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    WLBindingRouterSetCell *cell1 = (WLBindingRouterSetCell *)[self.tbWiFiSet cellForRowAtIndexPath:indexPath1];
    NSString *strPwd= cell1.textNetInfo.text;
    
    if (_wifiType==0)
    {
        //        _dictTwo[@"hasTwoConfig"] = [NSNumber numberWithBool:YES];
        _dictTwo[@"twoWifiAccount"] = strName;
        _dictTwo[@"twoWifiPassWord"] = strPwd;
    }
    else if (_wifiType==1)
    {
        //        _dictFive[@"hasFiveConfig"] = [NSNumber numberWithBool:YES];
        _dictFive[@"fiveAccount"] = strName;
        _dictFive[@"fiveWifiPassWord"] = strPwd;
        //        _dictFive = @{@"hasFiveConfig":[NSNumber numberWithBool:YES],@"fiveAccount":strName,@"fiveWifiPassWord":strPwd};
    }
    else
    {
        //        _dictGuest[@"hasCustomerConfig"] = [NSNumber numberWithBool:YES];
        _dictGuest[@"customerWifiAccount"] = strName;
        _dictGuest[@"customerWifiPassWord"] = strPwd;
    }
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    [self saveWiFiConfig];
    
    self.tbWiFiSet.frame = CGRectMake(WIDTH_SCREEN_WL*index, 0, WIDTH_SCREEN_WL, _tbWiFiSet.bounds.size.height);
    _wifiType = index;
    [self.tbWiFiSet reloadData];
    //侧滑手势处理
    //    if (_shouldHandleScreenEdgeGesture) {
    //        self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    //    }
    //    WLLog(@"____%d",index);
}

- (void)connectSaveAction
{
    [self.view endEditing:YES];
    NSString *strLineOne;
    NSString *strLineTwo;
    NSString *strLineThird;
    NSString *strLineFour;
    NSString *strLineFive;
    for (int i = 0; i<5; i++)
    {
        if (self.netType==0&&i>=2)
        {
            break;
        }
        if (self.netType==2) {
            if (i==0) {
                continue;
            }
            if (i>=3) {
                break;
            }
        }
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        WLBindingRouterSetCell *cell = (WLBindingRouterSetCell *)[self.tbNetwork cellForRowAtIndexPath:indexPath];
        if (i==0) {
            strLineOne = cell.textNetInfo.text;
        }
        else if (i==1)
        {
            strLineTwo = cell.textNetInfo.text;
        }
        else if (i==2)
        {
            strLineThird = cell.textNetInfo.text;
        }
        else if (i==3)
        {
            strLineFour = cell.textNetInfo.text;
        }
        else if (i==4)
        {
            strLineFive = cell.textNetInfo.text;
        }
    }
    NSDictionary *paramNet;
    WLWkSelf
    if (self.netType==0)
    {
        if (strLineOne.length<1) {
            [self showHUDMessage:@"请输入宽带帐号"];
            return;
        }
        else if (strLineTwo.length<1) {
            [self showHUDMessage:@"请输入宽带密码"];
        }
        NSDictionary *paramsp = @{@"networkAccount":strLineOne,@"networkPassWord":strLineTwo};
        [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"device/config/call" withParamsDic:paramsp success:^(NSDictionary *successDict) {
//            weakSelf.isFirstFinished = YES;
            [weakSelf updateBtn];
            [weakSelf showHUDMessage:successDict[@"message"]];
        } failure:^(NSString *errorMsg, NSInteger errorTag) {
            [weakSelf showHUDMessage:errorMsg];
        } error:^(NSError *error) {
            
        } inView:self.view];
        return;
    }
    else if (self.netType==1)
    {
        if (strLineOne.length<1) {
            [self showHUDMessage:@"请输入网络地址"];
            return;
        }
        else if (strLineTwo.length<1) {
            [self showHUDMessage:@"请输入子网掩码"];
            return;
        }
        else if (strLineThird.length<1) {
            [self showHUDMessage:@"请输入默认网关"];
            return;
        }
        else if (strLineFour.length<1) {
            [self showHUDMessage:@"请输入首选DNS"];
            return;
        }
        paramNet = @{@"sord":@"static",@"static_webAddress":strLineOne,@"static_subnetMask":strLineTwo,@"static_defaultNetWay":strLineThird,@"oneDNS":strLineFour,@"twoDNS":strLineFive};
    }
    else if (self.netType==2)
    {
        if (_netConfig==0) {
            if (strLineTwo.length<1) {
                [self showHUDMessage:@"未获取到DNS"];
                return;
            }
        }
        else
        {
            if (strLineTwo.length<1) {
                [self showHUDMessage:@"请输入首选DNS"];
                return;
            }
        }
        
        paramNet = @{@"sord":@"dynamic",@"dynamicType":@(_netConfig),@"oneDNS":strLineTwo,@"twoDNS":strLineThird};
    }
    
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"device/config/edit" withParamsDic:paramNet success:^(NSDictionary *successDict) {
//        weakSelf.isFirstFinished = YES;
        [weakSelf updateBtn];
        [weakSelf showHUDMessage:successDict[@"message"]];
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        [weakSelf showHUDMessage:errorMsg];
    } error:^(NSError *error) {
        
    } inView:self.view];
}

- (void)setDoneAction
{
    [self.view endEditing:YES];
    [self saveWiFiConfig];
    /*
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    WLBindingRouterSetCell *cell = (WLBindingRouterSetCell *)[self.tbWiFiSet cellForRowAtIndexPath:indexPath];
    NSString *strName= cell.textNetInfo.text;
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    WLBindingRouterSetCell *cell1 = (WLBindingRouterSetCell *)[self.tbWiFiSet cellForRowAtIndexPath:indexPath1];
    NSString *strPwd= cell1.textNetInfo.text;
    if (strName.length<1) {
        [self showHUDMessage:@"请输入WiFi名称"];
        return;
    }
    else if (strPwd.length<1) {
        [self showHUDMessage:@"请输入WiFi密码"];
    }
    
    NSString *strWap = @"psk2";
    if (_encrypType==2) {
        strWap = @"none";
    }
    else if (_encrypType==1)
    {
        strWap = @"psk-mixed";
    }
    NSDictionary *paramWiFi;
    if (_wifiType==0) {
        paramWiFi = @{@"hasTwoConfig":[NSNumber numberWithBool:YES],@"twoWifiPassWord":strPwd,@"twoWifiAccount":strName,@"twoWifiForce":@(_isHide),@"twoWifiLocktype":strWap};
    }
    else if (_wifiType==1)
    {
        paramWiFi = @{@"hasFiveConfig":[NSNumber numberWithBool:YES],@"fiveAccount":strName,@"fiveWifiPassWord":strPwd,@"fiveWifiLocktype":strWap,@"fiveWifiForce":@(_isHide)};
    }
    else
    {
        paramWiFi = @{@"hasCustomerConfig":[NSNumber numberWithBool:YES],@"customerWifiAccount":strName,@"customerWifiPassWord":strPwd,@"customerWifiLocktype":strWap,@"customerWifiForce":@(_isHide)};
    }
    */
    if ([_dictTwo[@"twoWifiAccount"] length]<1||[_dictTwo[@"twoWifiPassWord"] length]<1) {
        [self showHUDMessage:@"请设置2.4GWiFi"];
        return;
    }
    else if ([_dictFive[@"fiveAccount"] length]<1||[_dictFive[@"fiveWifiPassWord"] length]<1) {
        [self showHUDMessage:@"请设置5GWiFi"];
        return;
    }
    else if ([_dictGuest[@"customerWifiAccount"] length]<1||[_dictGuest[@"customerWifiPassWord"] length]<1) {
        [self showHUDMessage:@"请设置访客WiFi"];
        return;
    }
    
    NSMutableDictionary *paramWiFi = [[NSMutableDictionary alloc] initWithDictionary:_dictTwo];
    [paramWiFi addEntriesFromDictionary:_dictFive];
    [paramWiFi addEntriesFromDictionary:_dictGuest];
    WLWkSelf
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"device/config/edit" withParamsDic:paramWiFi success:^(NSDictionary *successDict) {
        [weakSelf showHUDMessage:successDict[@"message"]];
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        [weakSelf showHUDMessage:errorMsg];
    } error:^(NSError *error) {
        
    } inView:self.view];
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (_openFlag==0)
    {
        if (tap.view.tag==1) {
            _openFlag = 1;
            [_tbRouterSet reloadData];
        }
    }
    else if (_openFlag==1)
    {
        if (tap.view.tag==0) {
            _openFlag = 0;
            [_tbRouterSet reloadData];
        }
    }
//    if ([_isExpandArray[tap.view.tag] isEqualToString:@"0"]) {
//        //关闭 => 展开
//        //         [_isExpandArray removeObjectAtIndex:tap.view.tag];
//        //         [_isExpandArray insertObject:@"1" atIndex:tap.view.tag];
//        [_isExpandArray replaceObjectAtIndex:tap.view.tag withObject:@"1"];
//    }else{
//        //展开 => 关闭
//        //         [_isExpandArray removeObjectAtIndex:tap.view.tag];
//        //         [_isExpandArray insertObject:@"0" atIndex:tap.view.tag];
//        [_isExpandArray replaceObjectAtIndex:tap.view.tag withObject:@"0"];
//
//    }
//    NSIndexSet *set = [NSIndexSet indexSetWithIndex:tap.view.tag];
//    [_provinceTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
